package com.h5ve.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.h5ve.base.api.R;
import com.h5ve.base.util.StrUtil;
import com.h5ve.system.entity.Area;
import com.h5ve.system.service.AreaService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author h5ve
 * @date 2019-10-11 09:13:04
 * @description 区域Controller
 */
@RestController
@AllArgsConstructor
@RequestMapping("/system/area")
public class AreaController {

    private final AreaService areaService;

    private QueryWrapper<Area> getQueryWrapper(Area area) {
        return new QueryWrapper<Area>().like(StrUtil.isNotBlank(area.getName()), "name", area.getName())
                .orderByAsc("sort");
    }

//    @SaCheckPermission("area_view")
    @GetMapping("/list")
    public R list(Area area) {
        List<Area> classifyList = areaService.list(getQueryWrapper(area));
        return R.ok(classifyList);
    }

    @SaCheckPermission("area_view")
    @GetMapping("/listS")
    public R listS() {
        //Area area = areaService.getOne(new QueryWrapper<Area>().eq("parent_id", 0));
        return R.ok(areaService.list());
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(areaService.getById(id));
    }

    @SaCheckPermission("area_add")
    @PostMapping("/save")
    public R save(@RequestBody Area area) {
        if (area.getId() == null) {
            area.setParentId(0l);
            area.setParentIds("0");
            area.setType("0");
        }
        areaService.save(area);
        return R.ok();
    }

    @SaCheckPermission("area_edit")
    @PutMapping("/update")
    public R update(@RequestBody Area area) {
        areaService.updateById(area);
        return R.ok();
    }

    @SaCheckPermission("area_del")
    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable("id") Integer id) {
        return R.ok(areaService.removeById(id));
    }
}
